<?php
//require_once "../../app/dao/database.php";
//$class_db = new Database;

class Search{

	//var $query;
	var $db;
	
	function __construct(Database $db){
        $this->db = $db;
    }

	function search($query=null,$posisi=null, $batas=null){
	
		$this->db->connect->connect();
		
		$query = $_GET['keyword']; 
     
		$min_length = 3;
     
		if(strlen($query) >= $min_length){ 
         
			$query = htmlspecialchars($query); 
                 
			// SQL injection
			$query = mysql_real_escape_string($query);
         
			$raw_results = mysql_query("SELECT 
										product_detail.productdet_id as id,
										product_detail.product_name as name,
										product_detail.image as image,
										product.product_name as type,
										store.store_id as store_id,                    
										store.store_name as store,
										product_detail.price as price 
										FROM 
										product_category INNER JOIN product ON product_category.productcat_id = product.category
										INNER JOIN product_detail ON product.product_id = product_detail.product_id
										INNER JOIN store ON product_detail.store_id = store.store_id
										WHERE 
										product_detail.product_name
										LIKE 
										'%".$query."%'
										LIMIT $posisi,$batas;
										");
			//print_r($raw_results);
			if(mysql_num_rows($raw_results) > 0){ 
				
				while($r = mysql_fetch_array($raw_results)){
				
					//echo "<p><h3>".$results['name']."</h3>".$results['description']."</p>";
					$class_db = new Database;
					$search = new Search($class_db);
					
					$search->id = $r['id'];
					$search->name = $r['name'];
					$search->image = $r['image'];
					$search->type = $r['type'];
					$search->store_id = $r['store_id'];
					$search->store = $r['store'];
					$search->price = $r['price'];
					
					$this->search[] =$search;
					
				}
				
				$this->db->connect->disconnect();
				return $this->search;
				
			}else{ 
				
				$this->error = 'data tidak ditemukan';
                return $this->error;
				echo "No results";
			}
         
		}else{ 
			//echo "Minimum length is ".$min_length;
			$this->error = 'Minimum length is' .$min_length;
            return $this->error;
		}
	
	}
	
	

}
?>